package com.hunliji.monitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hunliji.common.response.PageData;
import com.hunliji.monitor.dao.mapper.mt.BusinessChangeAffirmOrderMapper;
import com.hunliji.monitor.dao.model.dataobject.BusinessChangeAffirmOrderDO;
import com.hunliji.monitor.dao.model.dataobject.GroupStatisticsDO;
import com.hunliji.monitor.dao.model.param.QueryOrderAffirmCountGroupDataParam;
import com.hunliji.monitor.service.api.BusinessChangeOrderAffirmService;
import com.hunliji.monitor.service.api.model.bo.BusinessChangeAffirmOrderBO;
import com.hunliji.monitor.service.api.model.bo.BusinessChangeOrderBO;
import com.hunliji.monitor.service.api.model.param.QueryChangeAffirmOrderParam;
import com.hunliji.monitor.service.api.model.param.QueryOrderAffirmCountGroupParam;
import com.hunliji.monitor.share.component.ComponentLocator;
import com.hunliji.monitor.share.utils.PageDataUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 变更确认单
 * @author wuchen
 */
@Service
public class BusinessChangeOrderAffirmServiceImpl implements BusinessChangeOrderAffirmService {
    @Resource
    private BusinessChangeAffirmOrderMapper businessChangeAffirmOrderMapper;
    @Override
    public BusinessChangeAffirmOrderBO add(BusinessChangeAffirmOrderBO businessChangeAffirmOrderBO) {
        BusinessChangeAffirmOrderDO businessChangeAffirmOrderDO = ComponentLocator.getConverter().convert(businessChangeAffirmOrderBO,BusinessChangeAffirmOrderDO.class);
        businessChangeAffirmOrderMapper.insert(businessChangeAffirmOrderDO);
        businessChangeAffirmOrderBO.setId(businessChangeAffirmOrderDO.getId());
        return businessChangeAffirmOrderBO;
    }

    @Override
    public void update(BusinessChangeAffirmOrderBO businessChangeAffirmOrderBO) {
        BusinessChangeAffirmOrderDO businessChangeAffirmOrderDO = ComponentLocator.getConverter().convert(businessChangeAffirmOrderBO,BusinessChangeAffirmOrderDO.class);
        businessChangeAffirmOrderMapper.updateById(businessChangeAffirmOrderDO);
    }

    @Override
    public BusinessChangeAffirmOrderBO queryById(Long id) {
        BusinessChangeAffirmOrderDO changeAffirmOrderDO = businessChangeAffirmOrderMapper.selectById(id);
        if(Objects.isNull(changeAffirmOrderDO)){
            return null;
        }
        return ComponentLocator.getConverter().convert(changeAffirmOrderDO,BusinessChangeAffirmOrderBO.class);
    }

    @Override
    public List<BusinessChangeAffirmOrderBO> query(QueryChangeAffirmOrderParam queryChangeAffirmOrderParam) {
        LambdaQueryWrapper<BusinessChangeAffirmOrderDO> queryWrapper = new LambdaQueryWrapper<BusinessChangeAffirmOrderDO>();
        queryWrapper.eq(BusinessChangeAffirmOrderDO::getDeleted,Boolean.FALSE);
        if(Objects.nonNull(queryChangeAffirmOrderParam.getChangeOrderId())){
            queryWrapper.eq(BusinessChangeAffirmOrderDO::getChangeOrderId,queryChangeAffirmOrderParam.getChangeOrderId());
        }
        List<BusinessChangeAffirmOrderDO> businessChangeAffirmOrderDOList = businessChangeAffirmOrderMapper.selectList(queryWrapper);
        return ComponentLocator.getConverter().convertList(businessChangeAffirmOrderDOList,BusinessChangeAffirmOrderBO.class);
    }

    @Override
    public PageData<BusinessChangeAffirmOrderBO> queryPage(QueryChangeAffirmOrderParam queryChangeAffirmOrderParam) {
        LambdaQueryWrapper<BusinessChangeAffirmOrderDO> queryWrapper = new LambdaQueryWrapper<BusinessChangeAffirmOrderDO>();
        queryWrapper.eq(BusinessChangeAffirmOrderDO::getDeleted,Boolean.FALSE);
        if(Objects.nonNull(queryChangeAffirmOrderParam.getChangeOrderId())){
            queryWrapper.eq(BusinessChangeAffirmOrderDO::getChangeOrderId,queryChangeAffirmOrderParam.getChangeOrderId());
        }
        if(Objects.nonNull(queryChangeAffirmOrderParam.getAffirmStatus())){
            queryWrapper.eq(BusinessChangeAffirmOrderDO::getAffirmStatus,queryChangeAffirmOrderParam.getAffirmStatus());
        }
        if(Objects.nonNull(queryChangeAffirmOrderParam.getUserId())){
            queryWrapper.eq(BusinessChangeAffirmOrderDO::getUserId,queryChangeAffirmOrderParam.getUserId());
        }
        queryWrapper.orderByDesc(BusinessChangeAffirmOrderDO::getCreatedAt);
        PageHelper.startPage(queryChangeAffirmOrderParam.getPageNum(),queryChangeAffirmOrderParam.getPageSize());
        List<BusinessChangeAffirmOrderDO> changeAffirmOrderDOList = businessChangeAffirmOrderMapper.selectList(queryWrapper);
        PageInfo<BusinessChangeAffirmOrderDO> pageInfo = new PageInfo<>(changeAffirmOrderDOList);
        if(CollectionUtils.isEmpty(changeAffirmOrderDOList)){
            return PageDataUtil.getEmptyData();
        }
        List<BusinessChangeAffirmOrderBO> changeAffirmOrderBOList = ComponentLocator.getConverter().convertList(changeAffirmOrderDOList,BusinessChangeAffirmOrderBO.class);
        return PageDataUtil.getPageData((int)pageInfo.getTotal(),changeAffirmOrderBOList);


    }

    @Override
    public Map<Long, Long> queryGroupCount(QueryOrderAffirmCountGroupParam queryOrderAffirmCountGroupParam) {
        QueryOrderAffirmCountGroupDataParam queryOrderAffirmCountGroupDataParam = ComponentLocator.getConverter().convert(queryOrderAffirmCountGroupParam,QueryOrderAffirmCountGroupDataParam.class);
        List<GroupStatisticsDO> groupStatisticsDOList = businessChangeAffirmOrderMapper.queryCountGroup(queryOrderAffirmCountGroupDataParam);
        Map<Long,Long> groupCountMap = new HashMap<>();
        if(CollectionUtils.isEmpty(groupStatisticsDOList)){
            return groupCountMap;
        }
        for (GroupStatisticsDO groupStatisticsDO:groupStatisticsDOList){
            groupCountMap.put(groupStatisticsDO.getStatisticsKey(),groupStatisticsDO.getStatisticsCount());
        }
        return groupCountMap;
    }
}
